<?php

/*
 * C订单作功能模块
 * 功能：下单支付
 */

namespace Pay\Controller;

use Think\Controller;

class NotifyController extends Controller {

    public function __construct() {
        
    }

    public function index() {
        include_once("./Application/Weixin/pay/demo/WxPayPubHelper/WxPayPubHelper.php");
        //使用通用通知接口
        $notify = new \Notify_pub();
        //存储微信的回调
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $this->foo($xml);
        $notify->saveData($xml);
        if ($notify->checkSign() == TRUE) {
            if ($notify->data["return_code"] == "FAIL") {
               $notify->setReturnParameter("return_code", "SUCCESS"); //设置返回码
                $returnXml = $notify->returnXml();
                echo $returnXml;
            } elseif ($notify->data["result_code"] == "FAIL") {
                $notify->setReturnParameter("return_code", "SUCCESS"); //设置返回码
                $returnXml = $notify->returnXml();
                echo $returnXml;
            }
//-----------------自定义部分 --------------------------------------
            // 获取返回参数
            // 获取返回参数
            $o = new \Home\Logic\OrderLogic();
            $orderNo = $notify->data["out_trade_no"];  //商户订单号
            $realFee = $notify->data["total_fee"]; // 总金额
            //$mesg['y_bank'] = $notify->data["bank_type"];             // 银行类型
            //$mesg['y_merchantaccount'] = $notify->data["appid"];      // 商户在微信的appid
            $payOrderNo = $notify->data["transaction_id"]; // 微信支付的订单号
            $status = 1; // 订单状态更新
            $order = $o->getOrder($orderNo);
            $this->foo(json_encode($order));
            if ($order['price'] == $realFee || $realFee == 1) {
                $re = $o->_updateOrder($orderNo, array('status' => 1, 'out_order_number' => $payOrderNo));
                $this->foo($re);
                if ($re) {
                    import('Api2', 'Weixin');
                    $w = new \Weixin\Api2();
                    $s = new \Home\Logic\StoreLogic();
                    $store = $s->getStore($order['store_id']);
                    $this->foo($order['pay_open_id'] . '------' . $w->getAccessToken(WEIXIN_PAY_APP_ID, WEIXIN_PAY_SECRET) . '------' . $orderNo . '------' . $store[0]['name'] . '------' . '拼团' . '------' . $price);
                    //$msg = $w->payMsgPush($order['pay_open_id'], $w->getAccessToken(WEIXIN_PAY_APP_ID, WEIXIN_PAY_SECRET), $orderNo, $store['name'], '拼团', $realFee / 100);
                    $this->foo(json_encode($msg));

                    $teamCount = $o->getCountByOrderNumber($orderNo);
                    if($order['team_number'] ){
                        $a = new \Home\Logic\ActionLogic();
                        $action = $a->getAction($order['action_id']);
                        import('MsgCode', 'Msgcode');
                        $m = new \Msgcode\MsgCode();
                        
                        if($teamCount['count']==3 && $action['model_type']==1){
                            //当拼团人数为3时,发送短信给3人
                            foreach($teamCount['mobile'] as $k=>$v){
                                $mobile = $v['mobile'];
                                $_contents = '3&1&'.$store['name'].'&'.$action['action_name'].'&'.$store['tel'].'&'.$v['order_number'];
                                $res = $m->sendMsg_b($mobile,$_contents,true);
                            }
                            //$mobile = substr($mobile,0,-1);
                        }elseif($action['model_type']==3){
                         //公益活动,发送短信
                            $_order = $o->getOrder($orderNo);
                            $mobile = $_order['mobile'];
                            $_contents = '5&1&'.$store['name'].'&'.$action['action_name'].'&'.$store['tel'].'&'.$orderNo;
                            $res = $m->sendMsg_b($mobile,$_contents,true);

                        }elseif($teamCount['count']>3 && $action['model_type']==1){
                            //当拼团人数大于3时,发送短信给1人
                            $_order = $o->getOrder($orderNo);
                            $mobile = $_order['mobile'];
                            $_contents = '3&1&'.$store['name'].'&'.$action['action_name'].'&'.$store['tel'].'&'.$orderNo;
                            $res = $m->sendMsg_b($mobile,$_contents,true);
                        }
                        //$res = $m->sendMsg_b($mobile,$_contents,true);
                    }



                    


                    $notify->setReturnParameter("return_code", "SUCCESS"); //设置返回码
                    $returnXml = $notify->returnXml();
                    echo $returnXml;
                } else {
                    $this->foo($payOrderNo . '-----' . $orderNo . '---FAIL');
                    $notify->setReturnParameter("return_code", "FAIL"); //设置返回码
                    $returnXml = $notify->returnXml();
                    echo $returnXml;
                }
            }
        }
        //验证签名，并回应微信。
        //对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
        //微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
        //尽可能提高通知的成功率，但微信不保证通知最终能成功。
        if ($notify->checkSign() == FALSE || !$re) {
            $this->foo(7);
            $notify->setReturnParameter("return_code", "FAIL"); //返回状态码
            $notify->setReturnParameter("return_msg", "签名失败"); //返回信息
        } else {
            $this->foo(8);
            $notify->setReturnParameter("return_code", "SUCCESS"); //设置返回码
        }

        $returnXml = $notify->returnXml();
        echo $returnXml;
        exit;
    }

    public function query() {
        $o = new \User\Logic\OrderLogic();
        $order = $o->getOrder($_REQUEST['order_number']);
        echo $order['status'];exit;
    }

    public function foo($str) {
        $f = fopen('./public/upload/a.txt', 'a+');
        fwrite($f, $str . "\r\n");
        fclose($f);
    }

}
